/**
 * Created by duwei on 2019/2/15.
 */
var mEffect = {
    /**
     * 亮张牌
     * @param showCard
     * @param userPos
     */
    liangPai: function (showCard, userPos) {
        // var img = showCard.getChildByName("img");
        var img = showCard;
        img.stopAllActions();
        showCard.stopAllActions();
        var begin_pos;
        var show_pos;
        var end_pos;
        begin_pos = cc.p(DesignSize.width / 2, 520);
        show_pos = cc.p(DesignSize.width / 2, 350);
        if (ZiPaiRoomData.playerNum < 4) {
            if (userPos == 0) {
                end_pos = cc.p(DesignSize.width / 2, 100);
            } else if (userPos == 1) {
                end_pos = cc.p(DesignSize.width - 100, 350);
            } else {
                end_pos = cc.p(100, 350);
            }
        } else {
            if (userPos == 0) {
                end_pos = cc.p(640, 100);
            } else if (userPos == 1) {
                end_pos = cc.p(1180, 350);
            } else if (userPos == 2) {
                end_pos = cc.p(640, 350);
            } else {
                end_pos = cc.p(100, 350);
            }
        }
        showCard.setPosition(begin_pos);
        // if (settingData.jisuchupai) {
        //
        // } else {
        img.setScale(0.5);
        showCard.runAction(cc.sequence(
            cc.moveTo(0.2, show_pos),
            cc.delayTime(0.5),
            cc.moveTo(0.2, end_pos),
            cc.callFunc(function () {
                showCard.setVisible(false);
            })
        ));
        img.runAction(cc.sequence(
            cc.scaleTo(0.2, 1),
            cc.delayTime(0.2 + 2),
            cc.scaleTo(0.2, 0.5)
        ));
        // }
    },
    /**
     * 出牌
     * @param showCard
     * @param userPos
     */
    chuPai: function (showCard, userPos) {
        // var img = showCard.getChildByName("img");
        var img = showCard;
        img.stopAllActions();
        showCard.stopAllActions();
        var begin_pos;
        var end_pos;
        var rotated = null;
        if (app.map_id.QYLDS == gameData.mapId || app.map_id.YZCHZ == gameData.mapId){
            if (ZiPaiRoomData.playerNum == 4) {
                if (userPos == 0) {
                    begin_pos = cc.p(DesignSize.width / 2, 100);
                    end_pos = cc.p(DesignSize.width / 2, 450);
                } else if (userPos == 1) {
                    begin_pos = cc.p(DesignSize.width - 64, 115);
                    end_pos = cc.p(DesignSize.width - 260, 270);
                } else if (userPos == 2) {
                    begin_pos = cc.p(DesignSize.width - 64, DesignSize.height - 56);
                    end_pos = cc.p(DesignSize.width - 260, DesignSize.height - 270);
                } else {
                    begin_pos = cc.p(64, DesignSize.height - 56);
                    end_pos = cc.p(260, DesignSize.height - 270);
                }
            }
            else if (ZiPaiRoomData.playerNum == 3) {
                if (userPos == 0) {
                    begin_pos = cc.p(DesignSize.width / 2, 100);
                    end_pos = cc.p(DesignSize.width / 2, 450);
                } else if (userPos == 1) {
                    begin_pos = cc.p(DesignSize.width - 64, DesignSize.height - 56);
                    end_pos = cc.p(DesignSize.width - 260, DesignSize.height - 270);
                } else {
                    begin_pos = cc.p(64, DesignSize.height - 56);
                    end_pos = cc.p(260, DesignSize.height - 270);
                }
            }
            else if (ZiPaiRoomData.playerNum == 2) {
                if (userPos == 0) {
                    begin_pos = cc.p(DesignSize.width / 2, 100);
                    end_pos = cc.p(DesignSize.width / 2, 450);
                } else {
                    begin_pos = cc.p(64, DesignSize.height - 56);
                    end_pos = cc.p(260, DesignSize.height - 270);
                }
            }
        }
        else if (ZiPaiRoomData.playerNum < 4) {
            if (userPos == 0) {
                begin_pos = cc.p(DesignSize.width / 2, 100);
                end_pos = cc.p(DesignSize.width / 2, 450);
            } else if (userPos == 1) {
                begin_pos = cc.p(DesignSize.width - 160, 350);
                end_pos = cc.p(DesignSize.width - 260, 450);
            } else {
                begin_pos = cc.p(160, 350);
                end_pos = cc.p(260, 450);
            }
        }
        else {
            /**
             * 4人玩处理
             * 衡阳六胡抢
             */
            if (ZiPaiRoomData.mapId == app.map_id.HENGYANGLHQ) {
                if (userPos == 0) {
                    begin_pos = cc.p(1280 / 2, 100);
                    end_pos = cc.p(1280 / 2, 450);
                    rotated = 90;
                } else if (userPos == 1) {
                    begin_pos = cc.p(1280 - 160, 150);
                    end_pos = cc.p(1280 - 260, 300);
                    rotated = 0;
                } else if (userPos == 2) {
                    begin_pos = cc.p(1280 - 160, 600);
                    end_pos = cc.p(1280 - 260, 500);
                    rotated = 90;
                } else {
                    begin_pos = cc.p(100, 600);
                    end_pos = cc.p(200, 500);
                       rotated = 0;
                }
            }
            else {
                if (userPos == 0) {
                    begin_pos = cc.p(640, 100);
                    end_pos = cc.p(500, 320);
                    rotated = 90;
                } else if (userPos == 1) {
                    begin_pos = cc.p(1180, 320);
                    end_pos = cc.p(800, 350);
                    rotated = 0;
                } else if (userPos == 2) {
                    begin_pos = cc.p(640, 500);
                    end_pos = cc.p(500, 510);
                    rotated = 90;
                } else {
                    begin_pos = cc.p(100, 350);
                    end_pos = cc.p(200, 350);
                    rotated = 0;
                }
            }
        }
        if (rotated != null) {
            showCard.setRotation(rotated);
        }
        if (settingData.jisuchupai) {
            img.setScale(1);
            showCard.setPosition(end_pos);
        } else {
            showCard.setPosition(begin_pos);
            img.setScale(0.5);
            showCard.runAction(cc.moveTo(0.2, end_pos));
            img.runAction(cc.scaleTo(0.2, 1));
        }
    },
    /**
     * 摸牌
     * @param showCard
     * @param userPos
     */
    moPai: function (showCard, userPos) {
        var img = showCard;
        img.stopAllActions();
        showCard.stopAllActions();
        var begin_pos = cc.p(1280 / 2, 580);
        var end_pos;
        var rotated = null;
        var userNum = 3;
        if (ZiPaiRoomData.options['shouxing'] && ZiPaiRoomData.ShouxingID > 0) {
            userNum = 4;
        }
        if (app.map_id.QYLDS == gameData.mapId || app.map_id.YZCHZ == gameData.mapId){
            if (ZiPaiRoomData.playerNum == 4) {
                if (userPos == 0) {
                    end_pos = cc.p(DesignSize.width / 2, 450);
                } else if (userPos == 1) {
                    end_pos = cc.p(DesignSize.width - 260, 270);
                } else if (userPos == 2) {
                    end_pos = cc.p(DesignSize.width - 260, DesignSize.height - 270);
                } else {
                    end_pos = cc.p(260, DesignSize.height - 270);
                }
            }
            if (ZiPaiRoomData.playerNum == 3) {
                if (userPos == 0) {
                    end_pos = cc.p(DesignSize.width / 2, 450);
                } else if (userPos == 1) {
                    end_pos = cc.p(DesignSize.width - 260, DesignSize.height - 270);
                } else {
                    end_pos = cc.p(260, DesignSize.height - 270);
                }
            }
            if (ZiPaiRoomData.playerNum == 2) {
                if (userPos == 0) {
                    end_pos = cc.p(DesignSize.width / 2, 450);
                } else {
                    end_pos = cc.p(260, DesignSize.height - 270);
                }
            }
        }
        else if (ZiPaiRoomData.playerNum < 4) {
            if (userPos == 0) {
                end_pos = cc.p(DesignSize.width / 2, 450);
            } else if (userPos == 1) {
                end_pos = cc.p(DesignSize.width - 260, 450);
            } else {
                end_pos = cc.p(260, 450);
            }
        } else {
            if (ZiPaiRoomData.mapId == app.map_id.HENGYANGLHQ) {
                if (userPos == 0) {
                    end_pos = cc.p(1280 / 2, 450);
                } else if (userPos == 1) {
                    end_pos = cc.p(1280 - 260, 300);
                } else if (userPos == 2) {
                    end_pos = cc.p(1280 - 260, 500);
                } else {
                    end_pos = cc.p(200, 500);
                }
            }
            else {
                if (userPos == 0) {
                    begin_pos = cc.p(640, 100);
                    end_pos = cc.p(500, 320);
                    rotated = 90;
                } else if (userPos == 1) {
                    begin_pos = cc.p(1180, 320);
                    end_pos = cc.p(800, 300);
                    rotated = 0;
                } else if (userPos == 2) {
                    begin_pos = cc.p(640, 500);
                    end_pos = cc.p(500, 510);
                    rotated = 90;
                } else {
                    begin_pos = cc.p(100, 350);
                    end_pos = cc.p(200, 350);
                    rotated = 0;
                }
            }
        }
        if (rotated != null) {
            showCard.setRotation(rotated);
        }
        if (settingData.jisuchupai) {
            img.setScale(1);
            showCard.setPosition(end_pos);
        }else{
            showCard.setPosition(begin_pos);
            img.setScale(0.5);
            showCard.runAction(cc.moveTo(0.15, end_pos));
            img.runAction(cc.scaleTo(0.15, 1));
        }
    },
    handleGuangquanAni:function(game_zhang,callback) {
        var ani_name = zipai.res.sp_fanxing ;
        if(ZiPaiRoomData.options['xingType']  == 1){
            ani_name = zipai.res.sp_genxing ;
        }
        var pos =  game_zhang.getPosition();
        var guangquan = playAnimScene(game_zhang.getParent(),ani_name, pos.x, pos.y, false,function () {
            guangquan.removeFromParent();
            if(callback){
                callback();
            }
        });
    },
    fanhu:function (cardID,selfparent,hupaiId,exit, isSelfHu, isShowHupaiId, isWangfanhu) {
        var game_zhang = selfparent;
        var pos =  game_zhang.getPosition();
        if(hupaiId && hupaiId != 21 && isSelfHu == true && isShowHupaiId == true){
            var hucard = new Card();
            // fanxingCard.setAnchorPoint(0, 1);
            hucard.setData(hupaiId, null, 1);
            hucard.setScale(1);
            hucard.setName('hucard');
            game_zhang.getParent().addChild(hucard);
            hucard.setPosition(cc.p(pos.x,pos.y-170));
            /*
            hucard.setVisible(false);
            var handleRemoveHucard = function (){
                hucard.setVisible(true);
            };
            hucard.runAction(
                cc.sequence(
                    cc.delayTime(0.5),
                    cc.callFunc(function () {
                        handleRemoveHucard();
                    })
                )
            )

             */
        }

    },
/**
     * 翻醒
     * @param cardID
     * @param _selfparent
     */
    //fanxing:function (cardID,selfparent,hupaiId,exit, isShowHupaiId) {
    fanxing:function (cardID,selfparent,hupaiId,exit, isSelfHu, isShowHupaiId, isWangfanhu) {
        if(exit){
            if(selfparent.getParent().getChildByName('fanxingCard')){
                selfparent.getParent().getChildByName('fanxingCard').removeFromParent();
            }
            if(selfparent.getParent().getChildByName('card')){
                selfparent.getParent().getChildByName('card').removeFromParent();
            }
            if(selfparent.getParent().getChildByName('hucard')){
                selfparent.getParent().getChildByName('hucard').removeFromParent();
            }
            return ;
        }


        var game_zhang = selfparent;
        var pos =  game_zhang.getPosition();

        var scale  =  0.8;
        var fanxingCard = new Card();
        // fanxingCard.setAnchorPoint(0, 1);
        fanxingCard.setData(0, null, 1);
        fanxingCard.setScale(0.7);
        fanxingCard.setName('fanxingCard');
        game_zhang.getParent().addChild(fanxingCard);


        fanxingCard.setPosition(cc.p(pos.x,pos.y-45));
        fanxingCard.setVisible(true);
        var flipTime = 0.2;
        var flipTimeEnd = 1;
        fanxingCard.runAction(cc.sequence(
            cc.spawn(
                cc.scaleTo(flipTime, 0,scale),
                cc.skewTo(flipTime, 0, 30)
            ),
            cc.callFunc(function () {
                fanxingCard.setVisible(false);
            }),
            cc.spawn(
                cc.scaleTo(flipTime,scale,scale),
                cc.skewTo(flipTime, 0, 0)
            )
        ));

        var card = new Card();
        card.setName('card');
        // fanxingCard.setAnchorPoint(0, 1);
        var xingCardId = deepCopy(cardID);
        if (hupaiId == 21) xingCardId = 21;
        else xingCardId = cardID
        card.setData(xingCardId, null, 1);
        card.setVisible(false);
        card.setPosition(cc.p(pos.x,pos.y-45));
        game_zhang.getParent().addChild(card);
        //card.runAction(cc.scaleTo(flipTime, 0, 1));
        var removeCard = function () {
            setTimeout(function () {
                fanxingCard.removeFromParent();
                // card.removeFromParent();
            },500)
        };
        card.runAction(cc.sequence(
            cc.spawn(
                cc.scaleTo(flipTime, 0, scale),
                cc.skewTo(flipTime, 0, 30)
            ),
            cc.callFunc(function () {
                card.setVisible(true);
            }),
            cc.spawn(
                cc.scaleTo(flipTime,scale, scale),
                cc.skewTo(flipTime, 0, 0)
            ),
            cc.callFunc(function () {
                removeCard();
            })
        ));

    },
    /**
     * 吃 碰
     * @param openCards
     * @param showPai
     */
    // chiPai: function (openCards, showPai) {
    //     var colIndex = openCards.getColIndex();
    //     var children = openCards.getChildren();
    //     for (var i = 0; i < children.length; i++) {
    //         var child = children[i];
    //         if (child.getTag() == colIndex) {
    //             child.setPositionX(child.getPositionX() + ((openCards.getPositionX() >= 1280 / 2) ? -250 : 250));
    //             var action = cc.sequence(
    //                 cc.moveBy(0.35, (openCards.getPositionX() >= 1280 / 2) ? 250 : -250, 0)
    //             );
    //             child.runAction(action);
    //         }
    //     }
    // },

    // 手牌   吃的哪几张牌
    chiPai: function (openCards, userPos, ishide, inData) {

        if(mRoom.isReplay || userPos!= 0){
            return;
        }
        var colIndex = openCards.getColIndex();
        if (inData['op'] != 1 && userPos == 0){
            if (inData['dui_arr']) {
                for (var j = 0; j < inData['dui_arr'].length; j++) {
                    var menzi = inData['dui_arr'][j];
                    var cards = menzi['pai_arr'];
                    if (cards.length == 4){
                        if (cards[0] == inData['pai_id']) {
                            colIndex = j + 1;
                            break;
                        }
                    }
                    if (cards.length == 3){
                        if (cards[0] == inData['pai_id'] && cards[0] === cards[1] && cards[0] == cards[2]) {
                            colIndex = j + 1;
                            break;
                        }
                    }
                }
            }
        }
        var children = openCards.getChildren();
        var posY = 0;
        var child0 = null, child1 = null, child2 = null, child3 = null;
        for (var i = 0; i < children.length; i++) {
            var child = children[i];
            if (child.getTag() == colIndex) {
                child.hideChiCard();
                if (userPos == 0) {
                    if (child.getName() == 'card0') {
                        posY = child.getPositionY();
                    }
                    if (!ishide)
                        child.setData(child.data, null, 1);
                    if (ishide)
                        child.setOpacity(160);
                    child.setPositionX(child.getPositionX() + 605 - colIndex * 50);
                    if (ishide) {
                        child.setPositionY(child.getPositionY() + 190);
                    } else {
                        child.setPositionY(posY + 190);
                    }

                    var action = cc.sequence(
                        cc.delayTime(.35)
                        , cc.moveBy(.15, -605 + colIndex * 50, -190)
                    );
                    child.runAction(action);
                    
                    if (ishide)
                        continue;
                    if (child.getName() == 'card0') {
                        child0 = child;
                        child0.setOpacity(ishide ? 160 : 255);
                        child0.runAction(cc.sequence(
                            cc.delayTime(.35),
                            cc.scaleTo(.15, .35),
                            cc.callFunc(function () {
                                for (var i = 0; i < children.length; i++) {
                                    var child_0 = children[i];
                                    if (child_0.getTag() == colIndex) {
                                        child_0.setData(child_0.data, null, 2);
                                        if (ishide)
                                            child_0.setOpacity(160);
                                        child_0.setScale(1);
                                        child_0.showChiCard();
                                    }
                                }
                            })
                        ));
                    }
                    if (child.getName() == 'card1') {
                        child1 = child;
                        child1.runAction(cc.moveBy(.15, 0, 70));
                        child1.runAction(cc.sequence(
                            cc.delayTime(.35),
                            cc.scaleTo(.15, .35),
                            cc.callFunc(function () {
                                child1.setScale(1);
                                child1.setOpacity(ishide ? 160 : 255);
                                child1.showChiCard();
                                // child1.setPositionY(child1.getPositionY() - 25);
                            })
                        ));
                        child1.runAction(cc.sequence(
                            cc.delayTime(.35),
                            cc.moveBy(.15, 0, - 28)
                        ));
                    }
                    if (child.getName() == 'card2') {
                        child2 = child;
                        child2.runAction(cc.moveBy(.15, 0, 140));
                        child2.runAction(cc.sequence(
                            cc.delayTime(.35),
                            cc.scaleTo(.15, .35),
                            cc.callFunc(function () {
                                child2.setScale(1);
                                child2.setOpacity(ishide ? 160 : 255);
                                child2.showChiCard();
                                // child2.setPositionY(child2.getPositionY() - 50);
                            })
                        ));
                        child2.runAction(cc.sequence(
                            cc.delayTime(.35),
                            cc.moveBy(.15, 0, -56)
                        ));
                    }
                    if (child.getName() == 'card3') {
                        child3 = child;
                        child3.runAction(cc.moveBy(.15, 0, 210));
                        child3.runAction(cc.sequence(
                            cc.delayTime(.35),
                            cc.scaleTo(.15, .35),
                            cc.callFunc(function () {
                                child3.setScale(1);
                                child3.setOpacity(ishide ? 160 : 255);
                                child3.showChiCard();
                                // child3.setPositionY(child3.getPositionY() - 75);
                            })
                        ));
                        child3.runAction(cc.sequence(
                            cc.delayTime(.35),
                            cc.moveBy(.15, 0, -84)
                        ));
                    }
                } else {
                    if (child.getName() == 'card0') {
                        posY = child.getPositionY();
                    }
                    if (ishide) {
                        //child.setData(child.data, null, 1, true);
                    } else {
                        child.setData(child.data, null, 1);
                    }
                    child.setPositionX(child.getPositionX() + ((openCards.getPositionX() >= 1280 / 2) ? -250 : 250));
                    if (!ishide)
                        child.setPositionY(posY);
                    var action = cc.sequence(
                        cc.delayTime(.35)
                        , cc.moveBy(.15, (openCards.getPositionX() >= 1280 / 2) ? 250 : -250, 0)
                        // ,cc.scaleTo(0.1, 1.5)
                        // ,cc.scaleTo(0.1, 1)
                    );
                    // child.stopAllActions();
                    child.runAction(action);
                    // child.runAction(cc.sequence(
                    //     cc.delayTime(.35),
                    //     cc.fadeTo(.10, 255)
                    // ));
                    if (ishide)
                        continue;
                    if (child.getName() == 'card0') {
                        child0 = child;
                        child0.runAction(cc.sequence(
                            cc.delayTime(.35),
                            cc.scaleTo(.15, .35),
                            cc.callFunc(function () {
                                for (var i = 0; i < children.length; i++) {
                                    var child_0 = children[i];
                                    if (child_0.getTag() == colIndex) {
                                        if (ishide) {
                                            child_0.setData(child_0.data, null, 2, true);
                                        } else {
                                            child_0.setData(child_0.data, null, 2);
                                        }
                                        child_0.setScale(1.1);
                                        child_0.showChiCard();
                                    }
                                }
                            })
                        ));
                    }
                    if (child.getName() == 'card1') {
                        child1 = child;
                        child1.runAction(cc.moveBy(.15, 0, 70));
                        child1.runAction(cc.sequence(
                            cc.delayTime(.35),
                            cc.scaleTo(.15, .35),
                            cc.callFunc(function () {
                                child1.setScale(1.1);
                                child1.showChiCard();
                                //child1.setPositionY(child1.getPositionY() - 25);
                            })
                        ));
                        child1.runAction(cc.sequence(
                            cc.delayTime(.35),
                            cc.moveBy(.15, 0, -25)
                        ));
                    }
                    if (child.getName() == 'card2') {
                        child2 = child;
                        child2.runAction(cc.moveBy(.15, 0, 140));
                        child2.runAction(cc.sequence(
                            cc.delayTime(.35),
                            cc.scaleTo(.15, .35),
                            cc.callFunc(function () {
                                child2.setScale(1.1);
                                child2.showChiCard();
                                //child2.setPositionY(child2.getPositionY() - 50);
                            })
                        ));
                        child2.runAction(cc.sequence(
                            cc.delayTime(.35),
                            cc.moveBy(.15, 0, -50)
                        ));
                    }
                    if (child.getName() == 'card3') {
                        child3 = child;
                        child3.runAction(cc.moveBy(.15, 0, 210));
                        child3.runAction(cc.sequence(
                            cc.delayTime(.35),
                            cc.scaleTo(.15, .35),
                            cc.callFunc(function () {
                                child3.setScale(1.1);
                                child3.showChiCard();
                                // child3.setPositionY(child3.getPositionY() - 75);
                            })
                        ));
                        child3.runAction(cc.sequence(
                            cc.delayTime(.35),
                            cc.moveBy(.15, 0, -75)
                        ));
                    }
                }
            }
        }
    },

};
